import {observable, action, toJS} from 'mobx';
import {WeaTableNew, WeaForm} from 'comsMobx';
import {WeaLocaleProvider} from "ecCom";
import {message, Modal} from "antd";
import _ from 'lodash';
import * as contractManagementApi from '../api/ContractManagementApi';
import {indexOf, isBlank, parseIntValue} from '../util/CommonUtil';
import LanguageConstant from '../constant/LanguageConstant';

const {getLabel} = WeaLocaleProvider;

class TableMatchingStore {

    /**
     * 是否有操作页面的权限
     *
     * @type {boolean}
     */
    @observable authorized = true;

    /**
     * 加载状态
     */
    @observable loading = false;

    /**
     * 流程模板设置关系id
     *
     * @type {number}
     */
    @observable workflowMouldRelationalId = -1;

    /**
     * 流程明细表id
     *
     * @type {number}
     */
    @observable workflowDetailTableId = -1;

    /**
     * 模板表格索引
     *
     * @type {number}
     */
    @observable mouldTableIndex = 0;

    /**
     * 表格替换范围
     *
     * @type {number}
     */
    @observable replaceStartRow = -1;

    /**
     * 表格替换范围
     *
     * @type {number}
     */
    @observable replaceEndRow = -1;

    /**
     * 表格替换范围选项
     *
     * @type {Array}
     */
    @observable replaceRowRangeOptions = [];

    /**
     * 流程明细表选择选项
     *
     * @type {Array}
     */
    @observable workflowDetailTableOptions = [];

    /**
     * 模板表格选择框选项
     * @type {Array}
     */
    @observable mouldTableOptions = [];

    /**
     * 流程字段选择框选项
     *
     * @type {Array}
     */
    @observable workflowFieldOptions = [];

    /**
     * 显示类型下拉选择框选项
     * @type {Array}
     */
    @observable showTypeOptions = {};

    /**
     * 模板列映射数据
     *
     * @type {Array}
     */
    @observable columnMappings = [];

    /**
     * 初始化话
     * @param param
     */
    @action
    init = (params = {}) => {
        const {workflowMouldRelationalId} = params;
        this.reset();
        this.workflowMouldRelationalId = workflowMouldRelationalId;
    }

    /**
     * 重置参数
     */
    @action
    reset = () => {
        this.loading = false;
        this.workflowDetailTableId = -1;
        this.workflowMouldRelationalId = -1;
        this.mouldTableIndex = -1;
        this.replaceEndRow = -1;
        this.replaceStartRow = -1;
        this.mouldTableOptions = [];
        this.workflowDetailTableOptions = [];
        this.columnMappings = [];
        this.workflowFieldOptions = [];
        this.replaceRowRangeOptions = [];
    }

    /**
     * 获取模板表格匹配编辑表单
     *
     * @param params
     */
    @action
    getMouldTableMatchingForm = (params = {}) => {
        const workflowMouldRelationalId = toJS(this.workflowMouldRelationalId);
        params = {workflowMouldRelationalId, ...params};
        contractManagementApi.getMouldTableMatchingForm(params).then(res => {
            const {code, data} = res;
            if (code == 200) {
                const {
                    mouldTableOptions, workflowDetailTableOptions, mouldTableIndex, workflowDetailTableId, replaceRowRangeOptions
                } = data;
                this.mouldTableOptions = mouldTableOptions;
                this.workflowDetailTableOptions = workflowDetailTableOptions;
                this.workflowDetailTableId = workflowDetailTableId;
                this.mouldTableIndex = mouldTableIndex;
                this.replaceRowRangeOptions = replaceRowRangeOptions;
                this.getMouldTableMatchingDatas();
            }
        });
    }

    /**
     * 获取模板表格列匹配数据
     *
     * @param params
     */
    @action
    getMouldTableMatchingDatas = (params = {}) => {
        const workflowMouldRelationalId = toJS(this.workflowMouldRelationalId);
        const workflowDetailTableId = toJS(this.workflowDetailTableId);
        const mouldTableIndex = toJS(this.mouldTableIndex);
        params = {workflowMouldRelationalId, workflowDetailTableId, mouldTableIndex, ...params};
        contractManagementApi.getMouldTableMatchingDatas(params).then(res => {
            const {code, data} = res;
            if (code == 200) {
                const {
                    workflowFieldOptions, columnMappings, showTypeOptions, replaceStartRow,
                    replaceEndRow, replaceRowRangeOptions
                } = data;
                this.workflowFieldOptions = workflowFieldOptions;
                this.columnMappings = columnMappings;
                this.showTypeOptions = showTypeOptions;
                this.replaceStartRow = replaceStartRow;
                this.replaceEndRow = replaceEndRow;
                this.replaceRowRangeOptions = replaceRowRangeOptions;
            }
        });
    }

    /**
     * 改变表单下拉选择框事件处理
     *
     * @param fieldname
     * @param val
     */
    @action
    changeFormSelect = (fieldname, val) => {
        if (fieldname == "mouldTableIndex") {
            this.mouldTableIndex = val;
        } else if (fieldname == "workflowDetailTableId") {
            this.workflowDetailTableId = val;
        } else if (fieldname == "replaceStartRow") {
            this.replaceStartRow = val;
        } else if (fieldname == "replaceEndRow") {
            this.replaceEndRow = val;
        }
        if (fieldname == "mouldTableIndex" || fieldname == "workflowDetailTableId") {
            this.columnMappings = [];
            this.workflowFieldOptions = [];
            this.getMouldTableMatchingDatas();
            this.replaceEndRow = -1;
            this.replaceStartRow = -1;
            this.replaceRowRangeOptions = [];
        }
    }


    /**
     * 模板表格列字段匹配表相关字段改变事件处理
     *
     * @param id
     * @param field
     * @param val
     * @param record
     */
    @action
    changeMouldTableMatchingTableField = (id, field, value, record) => {
        if (field != undefined) {
            const columnMappings = toJS(this.columnMappings);
            for (let i = 0; i < columnMappings.length; i++) {
                let item = columnMappings[i];
                if (item && item.id == id && value != undefined) {
                    // 流程字段变化时，清空显示设置选项
                    if (field == "fieldId" && item[field] != value) {
                        item.showType = "";
                        item.percentagePrecision = 2;
                        item.floatPrecision = 2;
                    }
                    item[field] = value;
                    break;
                }
            }
            this.columnMappings = columnMappings;
        }
    }

    /**
     * 保存模板表格匹配数据
     *
     * @param successCallback
     */
    @action
    saveMouldTableMatchingTable = (successCallback) => {
        const workflowMouldRelationalId = toJS(this.workflowMouldRelationalId);
        const workflowDetailTableId = toJS(this.workflowDetailTableId);
        const mouldTableIndex = toJS(this.mouldTableIndex);
        const replaceStartRow = toJS(this.replaceStartRow);
        const replaceEndRow = toJS(this.replaceEndRow);
        const columnMappings = JSON.stringify(toJS(this.columnMappings));
        const params = {
            workflowMouldRelationalId,
            workflowDetailTableId,
            mouldTableIndex,
            replaceEndRow,
            replaceStartRow,
            columnMappings
        };
        contractManagementApi.saveMouldTableMatchingDatas(params).then(res => {
            const {code} = res;
            if (code == 200) {
                message.success(getLabel(LanguageConstant.SAVE_SUCCESS, "保存成功"));
                if (_.isFunction(successCallback)) {
                    successCallback();
                }
            } else {
                message.error(getLabel(LanguageConstant.SAVE_FAIL, "保存失败"));
            }
        });
    }

}

export default TableMatchingStore;